package org.training.apr.model.utils.builder;

import org.training.apr.FakeProperties;
import org.training.apr.model.BusinessRuleViolation;
import org.training.apr.model.Enterprise;
import org.training.apr.model.PaymentOrder;
import org.training.apr.model.Vat;
import org.training.apr.security.FakeSecurityContext;

public class PaymentOrderBuilder extends Builder {
	private Enterprise enterprise = new EnterpriseBuilder().build();
	private Vat vat = new VatBuilder().build();
	private double rawValue = 122031;
	private boolean authorized = false;
	private boolean supervised = false;
	
	public PaymentOrderBuilder withEnterprise(Enterprise enterprise) {
		this.enterprise = enterprise;
		return this;
	}
	
	public PaymentOrderBuilder withVat(Vat vat) {
		this.vat = vat;
		return this;
	}
	
	public PaymentOrderBuilder withRawValue(double rawValue) {
		this.rawValue = rawValue;
		return this;
	}
	
	public PaymentOrderBuilder withAuthorized(boolean authorized) {
		this.authorized = authorized;
		return this;
	}
	
	public PaymentOrderBuilder withSupervised(boolean supervised) {
		this.supervised = supervised;
		return this;
	}

	public PaymentOrder build() {
		try {
			PaymentOrder order = new PaymentOrder();
			
			order.setId(id);
			order.setVat(vat);
			order.setEnterprise(enterprise);
			order.setRawValue(rawValue);	
			superuser();
			order.markAsSupervised(FakeSecurityContext.getInstance().getUser(), supervised);
			order.authorize(FakeProperties.getHostEnterprise(), FakeProperties.getSupervisionThreshold(), authorized);
			restoreUser();
			
			return order;
		} catch (BusinessRuleViolation e) {
			throw new RuntimeException(e);
		}
	}
	
	
}
